/*
 * Webskine administrator interface.
 *   http://dealmeida.net/projects/webskine
 *
 * This module is a client to my JSON store module, creating,
 * editing and deleting Atom entries.
 *
 * Copyright (c) 2006 Roberto De Almeida
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

/*
 * A JSON request, loosely based on http://www.json.org/JSONRequest.html.
 *
 * We could use http://json.org/json.js for parsing the JSON response,
 * but it hangs Firefox.
 */
$.json = function(type, url, data, ret) {
    var xml = new XMLHttpRequest();

    if (xml) {
        xml.open(type || "GET", url, true);

        xml.setRequestHeader('Accept', 'application/json');
        if (data) xml.setRequestHeader('Content-Type', 'application/json');

        xml.onreadystatechange = function() {
            if (xml.readyState == 4) {
                if (ret) {
                    try {
                        eval('var jsonResponse = ' + xml.responseText + ';');
                    } catch (err) {
                        jsonResponse = null;
                    }
                    ret(jsonResponse, xml.status);
                }
            }
        };
        xml.send(toJsonString(data));
    }
};
